package ar.edu.unicen.exa.isistan.archassis.view;

import java.io.FileNotFoundException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;

import swing2swt.layout.BorderLayout;
import ar.edu.unicen.exa.isistan.archassis.model.ModelManager;
import ar.edu.unicen.exa.isistan.archassis.utils.HTMLResource;
import ar.edu.unicen.exa.isistan.archassis.view.cases.CasesComposite;
import ar.edu.unicen.exa.isistan.archassis.view.classes.HTMLClassComposite;
import ar.edu.unicen.exa.isistan.archassis.view.rcs.SVNComposite;
import ar.edu.unicen.exa.isistan.archassis.view.toolbar.ToolBarUtil;

public class Application {
	public static final String APPLICATION_NAME = "ArchAssis";

	protected Shell shell;
	private SVNComposite versionComposite;
	private HTMLClassComposite classesComposite;
	private CasesComposite casesComposite;
	//private Composite composite;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			HTMLResource.buildHTMLWorkspace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			
			Application window = new Application();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(648, 389);
		shell.setText(APPLICATION_NAME);
		shell.setLayout(new BorderLayout(5, 5));
		
		Composite topPanel = new Composite(shell, SWT.NONE);
		topPanel.setLayoutData(BorderLayout.NORTH);
		topPanel.setBackground(SWTResourceManager.getColor(SWT.COLOR_RED));
		topPanel.setLayout(new BorderLayout(0, 0));
		
		versionComposite = new SVNComposite(topPanel);
		
		//SashForm sashForm = new SashForm(shell, SWT.BORDER | SWT.SMOOTH);
		//Composite composite = new Composite(shell, SWT.NONE);
//		SashForm sashForm = new SashForm(shell, SWT.BORDER | SWT.SMOOTH);
//		sashForm.setLayoutData(BorderLayout.CENTER);
		
		//composite.setLayout(new BorderLayout(0, 0));
		
		
//		Composite centralPanel = new Composite(composite, SWT.NONE);
//		centralPanel.setLayoutData(BorderLayout.CENTER);
//		centralPanel.setBackground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));
//		centralPanel.setLayout(new FillLayout(SWT.HORIZONTAL));
		classesComposite = new HTMLClassComposite(shell);
		classesComposite.setLayoutData(BorderLayout.CENTER);
		
//		Composite rightPanel = new Composite(sashForm, SWT.NONE);
//		rightPanel.setLayoutData(BorderLayout.EAST);
//		rightPanel.setBackground(SWTResourceManager.getColor(SWT.COLOR_GREEN));
//		rightPanel.setLayout(new FillLayout(SWT.HORIZONTAL));
//		casesComposite = new CasesComposite(sashForm);
		//sashForm.setMaximizedControl(classesComposite);
//		sashForm.setMaximizedControl(null);
		
		new ToolBarUtil(topPanel);
		
//		new SashListener(sashForm, classesComposite, casesComposite);
		
		ModelManager.getInstance().setClassesList(null);
		
	}
	
	public static void showErrorMessage(Shell shell, String text) {
	    int style = SWT.ICON_ERROR;
	    
	    //SWT.ICON_QUESTION | SWT.YES | SWT.NO
	    
	    MessageBox messageBox = new MessageBox(shell, style);
	    messageBox.setMessage(text);
	    int rc = messageBox.open();
	    
	}
}
